package com.entrogy.controller;

import com.entrogy.bean.PayFormEntity;
import com.entrogy.bean.PayOrderEntity;
import com.entrogy.bean.PayProduct;
import com.entrogy.bean.StudentEntity;
import com.entrogy.service.ParentService;
import com.entrogy.service.PayService;
import com.entrogy.utils.ApiReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xlr
 * @date 2019-07-16
 * @description 增值功能的付费功能模块
 */
@Api(description = "支付功能的接口模块（注意：以后把走读生改成住校生暂时不考虑）（点击支付模块--->家长对应的孩子列表---->点击支付按钮------>进入到新增功能产品介绍页面----->" +
        "确认支付---->调用统一下单接口------>返回前端调起支付接口---->查询订单（如果成功则去操作我们的家长下单表））")
@RestController
@RequestMapping(value = "/pay")
public class PayController {

    private static final Logger logger = LoggerFactory.getLogger(PayController.class);

    @Autowired
    private ParentService parentService;

    @Autowired
    private PayService payService;

    @ApiOperation(value = "老师或家长获取孩子的列表看那些孩子没有支付款(返回的字段isPay:1--已支付 0--未支付,payTime：开通时间,expiryDate:有效期)")
    @ApiImplicitParam(value = "用户ID", name = "uid", paramType = "query", dataType = "String", required = true)
    @RequestMapping(value = "/getstudentsbyparentid",method = RequestMethod.GET)
    public Map<String,Object> getStudentsByParentID(String uid){
         List<StudentEntity> lists = parentService.getStudentIsPayByPID(uid);
         return ApiReturnUtil.success("查询成功",lists);
    }

    @ApiOperation(value = "根据学生的allowTime查询出其对应的功能产品介绍页面")
    @ApiImplicitParam(value = "学生的allowTime", name = "allowTime", paramType = "query", dataType = "String", required = true)
    @RequestMapping(value = "/getpayproduct",method = RequestMethod.GET)
    public Map<String,Object> getPayProductByID(String allowTime){
        int productId = 1;// 住校生 商品ID 为1
        if(!allowTime.equals("000")){// 走读生  商品ID 2
           productId = 2;
        }
        PayProduct payProduct = payService.getPayProductByID(productId);
        return ApiReturnUtil.success("查询成功", payProduct);
    }

    @ApiOperation(value = "统一下单接口(必须要的参数如下：id:用户ID,studentId:学生ID,productId:商品ID,body:商品描述)")
    @RequestMapping(value = "/wxpay", method = RequestMethod.POST)
    public Map<String,Object> wxPay(PayFormEntity payForm){
          return payService.wxPay(payForm);
    }

    @ApiOperation(value = "支付回调接口")
    @RequestMapping(value = "/wxnotify", method = RequestMethod.POST)
    public String wxNotify(HttpServletRequest request){
        return payService.wxNotify(request);
    }

    @ApiOperation(value = "订单查询(orderId:订单号,expiryDate:有效期限,description:商品描述,productName:产品名称,orderTitle:订单标题(学生名字+产品名称),expiryDate:有效期限)")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户ID",name = "uid",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(value = "页码",name = "currentPage",paramType = "query",dataType = "Integer",required = true)
    })
    @RequestMapping(value = "/getorder",method = RequestMethod.GET)
    public Map<String,Object> getPayOrder(String uid,Integer currentPage){
        if(currentPage == null){
            currentPage = 1;
        }
        int pageSize = 5;//显示条数
        int beginIndex = currentPage * pageSize - pageSize;// 开始查询的SQL下标
        List<PayOrderEntity> payOrders = payService.getOrdersByUid(uid,beginIndex,pageSize);
        int totalCount = payService.getPayOrderTotalCount(uid);//根据家长ID查询出其所有的订单条数
        Map<String,Object> map = new HashMap<>();
        map.put("code",1);
        map.put("totalCount",totalCount);
        map.put("msg","查询成功");
        map.put("data",payOrders);
        return map;
    }

    @ApiOperation(value = "根据产品ID查询出其对应的功能产品介绍页面")
    @ApiImplicitParam(value = "产品ID",name = "pid",paramType = "query",dataType = "int",required = true)
    @RequestMapping(value = "/getpayproductbyid",method = RequestMethod.GET)
    public Map<String,Object> getPayProduct(int pid){
        PayProduct payProduct = payService.getPayProductByID(pid);
        return ApiReturnUtil.success("查询成功",payProduct);
    }


}
